local ToolBase = require("app.Base.LayerBase")

local MapTool = class("MapTool", ToolBase)

local CHOOSE_TYPE = {
    dian = 1,
    xian = 2,
    mian = 3,
}

function MapTool:ctor(data)
    MapTool.super.ctor(self,data,true)
    print("MapTool")

    self.vChooseTypeBtn = {}

    self:initUI()
end

function MapTool:onEnter()
    self:Initialization()
    self:AddButtonEvent()
    self:AddKeyboardEvent()
end

function MapTool:initUI()
--    MapTool.super.initUI(self)
    self.btn_dian = td.CreateBtn({text = "点",map = td.btnMap.huangse_zheng})
    self.btn_dian:addTo(self):move(-100,-50)
    self.btn_xian = td.CreateBtn({text = "线",map = td.btnMap.huangse_zheng})
    self.btn_xian:addTo(self):move(0,-50)
    self.btn_mian = td.CreateBtn({text = "面",map = td.btnMap.huangse_zheng})
    self.btn_mian:addTo(self):move(100,-50)

    table.insert(self.vChooseTypeBtn,self.btn_dian)
    table.insert(self.vChooseTypeBtn,self.btn_xian)
    table.insert(self.vChooseTypeBtn,self.btn_mian)   
    
    self.text_Ctrl = td.CreateLabel("Ctrl")
    self.text_Ctrl:addTo(self):move(-50,-50)
    self.text_Shift = td.CreateLabel("Shift")
    self.text_Shift:addTo(self):move(50,-50)
    self.text_Alt = td.CreateLabel("Alt")
    self.text_Alt:addTo(self):move(150,-50)

--    self.text_dikuaicanshu = td.CreateLabel("地块参数")
--    self.text_dikuaicanshu:addTo(self):move(0,-100)

--    self.text_zuobiao = td.CreateLabel("坐标：")
--    self.text_zuobiao:addTo(self):move(-50,-150)

--    self.text_grid = td.CreateLabel("X:0 Y:0")
--    self.text_grid:addTo(self):move(50,-150)

--    self.text_weizhi = td.CreateLabel("位置：")
--    self.text_weizhi:addTo(self):move(-50,-200)

--    self.text_pos = td.CreateLabel("X:0 Y:0")
--    self.text_pos:addTo(self):move(50,-200)

--    self.text_gaodu = td.CreateLabel("高度：")
--    self.text_gaodu:addTo(self):move(-50,-250)

--    self.text_height = td.CreateLabel("height:0")
--    self.text_height:addTo(self):move(50,-250)

--    self.text_dixing = td.CreateLabel("地形：")
--    self.text_dixing:addTo(self):move(-50,-300)

--    self.text_terrain = td.CreateLabel("terrain:无")
--    self.text_terrain:addTo(self):move(50,-300)
end

function MapTool:Initialization()
    self:ClickChooseType(CHOOSE_TYPE.dian)
end

function MapTool:AddButtonEvent()
    for i, btn in ipairs(self.vChooseTypeBtn) do
        btn:setTag(i)
        td.BtnAddTouch(btn, handler(self,self.onClickChooseTypeBtn)) 
    end
end

function MapTool:onClickChooseTypeBtn(btn)
    self:ClickChooseType(btn:getTag())
end

function MapTool:ClickChooseType(chooseType)
    self.choose_type = chooseType
    for i, btn in ipairs(self.vChooseTypeBtn) do
        if i == chooseType then 
            btn:setBright(true)
        else
            btn:setBright(false)
        end
    end    
end

function MapTool:AddKeyboardEvent()    
    --键盘事件  
    local function onKeyPressed(keyCode, event)  
--        local buf = string.format("%d键按下!", keyCode)  
--        print(buf)
        self:onKeyboard(keyCode,1,event)
    end  
  
    local function onKeyReleased(keyCode, event)        
--        local buf = string.format("%d键抬起!", keyCode)  
--        print(buf)
        self:onKeyboard(keyCode,2,event)
    end  
    
    local listener = cc.EventListenerKeyboard:create()  
    listener:registerScriptHandler(onKeyPressed, cc.Handler.EVENT_KEYBOARD_PRESSED)  
    listener:registerScriptHandler(onKeyReleased, cc.Handler.EVENT_KEYBOARD_RELEASED)  
  
    cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, self) 
end  

function MapTool:onKeyboard(keyCode,keyCodeType,event)
--    dump(keyCode,keyCodeType)
    local keyName,keyTypeName,key
    
    if keyCode == 14 then 
        keyName = "Ctrl"
        key = 1
    elseif keyCode == 12 then 
        keyName = "Shift"
        key = 2
    elseif keyCode == 16 then 
        keyName = "Alt"
        key = 3
    else    
        return
    end

    if keyCodeType == 1 then 
        self.lastKeydown = self.choose_type
        keyTypeName = "按下"
        self:ClickChooseType(key)
    elseif keyCodeType == 2 then
        keyTypeName = "弹起"
        if self.lastKeydown == nil then 
--            return 
        elseif self.lastKeydown == key then 
--            return
        else
            self:ClickChooseType(self.lastKeydown)                    
            self.lastKeydown = nil
        end
        
    end

    print(keyName.."被"..keyTypeName)

end


return MapTool